package cn.net.yto.school.web.student;

import cn.net.yto.base.dao.entities.TAccount;
import cn.net.yto.base.dao.pagination.PaginationRequest;
import cn.net.yto.base.dao.pagination.PaginationResponse;
import cn.net.yto.base.shiro.util.ShiroUserHelper;
import cn.net.yto.base.util.RequestHandler;
import cn.net.yto.school.dao.entities.TbClazz;
import cn.net.yto.school.dao.entities.TbCourse;
import cn.net.yto.school.dao.entities.TbCourseStudent;
import cn.net.yto.school.dao.entities.TbStudent;
import cn.net.yto.school.service.ClazzService;
import cn.net.yto.school.service.CourseService;
import cn.net.yto.school.service.CourseStudentService;
import cn.net.yto.school.service.StudentService;
import cn.net.yto.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private CourseStudentService courseStudentService;

    @Autowired
    private CourseService courseService;

    @RequestMapping("query")
    public String query(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "student/student_init";
    }

    @RequestMapping("list")
    public String list(HttpServletRequest request, HttpServletResponse response, Model model) {
        PaginationRequest paginationRequest = RequestHandler.buildPaginationRequest(request, model);
        PaginationResponse<TbStudent> paginationResponse = this.studentService.findResultsByPagination(paginationRequest);
        model.addAttribute("paginationResponse", paginationResponse);

        // 增加班级数据
        List<TbClazz> clses = this.clazzService.findAllClazz();
        model.addAttribute("clses", clses);

        return "student/student_list";
    }

    @RequestMapping("delete/{id}")
    public String delete(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable("id") Integer id){
        this.studentService.deleteStudentById(id);

        return "redirect:/student/query";
    }

    @RequestMapping(value = {"add"}, method = {RequestMethod.GET})
    public String addStudent(HttpServletRequest request, HttpServletResponse response, Model model) {
        // 增加班级数据
        List<TbClazz> clses = this.clazzService.findAllClazz();
        model.addAttribute("clses", clses);

        return "student/student_add";
    }

    @RequestMapping(value = {"add"}, method = {RequestMethod.POST})
    public String addClazzHandler(HttpServletRequest request, HttpServletResponse response, Model model, String stuBirthday, String clsId) {

        TbStudent student = (TbStudent) RequestHandler.createBean(request, new TbStudent());

        student.setStuBirth(Timestamp.valueOf(stuBirthday + " 00:00:00"));
        student.setClazz(new TbClazz(Integer.valueOf(clsId)));

        TAccount account = new TAccount();
        account.setId(ShiroUserHelper.getCurrentShiroUser().getId());
        student.setActor(account);

        this.studentService.saveStudent(student);

        return "redirect:/student/query";
    }

    @RequestMapping(value = {"edit/{id}"}, method = {RequestMethod.GET})
    public String editClazz(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable("id") Integer id) {

        model.addAttribute("student", this.studentService.findOne(id));

        // 增加班级数据
        List<TbClazz> clses = this.clazzService.findAllClazz();
        model.addAttribute("clses", clses);

        return "student/student_edit";
    }

    @RequestMapping(value = {"edit"}, method = {RequestMethod.POST})
    public String editClazzHandler(HttpServletRequest request, HttpServletResponse response, Model model, String stuBirthday, String clsId) {

        TbStudent student = (TbStudent) RequestHandler.createBean(request, new TbStudent());

        if(stuBirthday != null && !StringUtil.isEmpty(stuBirthday)){
            student.setStuBirth(Timestamp.valueOf(stuBirthday + " 00:00:00"));
        }
        student.setClazz(new TbClazz(Integer.valueOf(clsId)));

        //增加最后一次操作员数据
        TAccount account = new TAccount();
        account.setId(ShiroUserHelper.getCurrentShiroUser().getId());
        student.setActor(account);

        this.studentService.saveStudent(student);

        return "redirect:/student/query";
    }

    //学生选课页面跳转方法
    @RequestMapping(value = {"slctcourse"}, method = {RequestMethod.GET})
    public String selectCourse(HttpServletRequest request, HttpServletResponse response, Model model) {

        //增加学生数据
        //增加课程数据
        List<TbCourseStudent> courseStudents = this.courseStudentService.findAll();
        List<TbStudent> students = this.studentService.findAll();
        List<TbCourse> courses = this.courseService.findAll();

        model.addAttribute("courseStudents", courseStudents);
        model.addAttribute("students", students);
        model.addAttribute("courses", courses);

        return "student/student_slctcourse";
    }

    @RequestMapping(value = {"slctcourse"}, method = {RequestMethod.POST})
    public String selectCourseHandler(HttpServletRequest request, HttpServletResponse response, Model model) {

//        TbStudent student = (TbStudent) RequestHandler.createBean(request, new TbStudent());
//
//        student.setStuBirth(Timestamp.valueOf(stuBirthday + " 00:00:00"));
//        student.setClazz(new TbClazz(Integer.valueOf(clsId)));
//
//        TAccount account = new TAccount();
//        account.setId(ShiroUserHelper.getCurrentShiroUser().getId());
//        student.setActor(account);
//
//        this.studentService.saveStudent(student);

        return "redirect:/student/student_slctcourse";
    }
}
